/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.jcdev.coolrss.model.dao;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import net.jcdev.coolrss.model.dao.exceptions.NonexistentEntityException;
import net.jcdev.coolrss.model.entity.Description;
import net.jcdev.coolrss.model.entity.Entry;

/**
 *
 * @author carlos
 */
public class DescriptionJpaController {

    public DescriptionJpaController() {
        emf = Persistence.createEntityManagerFactory("CoolRSSPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Description description) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Entry entryId = description.getEntryId();
            if (entryId != null) {
                entryId = em.getReference(entryId.getClass(), entryId.getEntryId());
                description.setEntryId(entryId);
            }
            em.persist(description);
            if (entryId != null) {
                entryId.getDescriptionList().add(description);
                entryId = em.merge(entryId);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Description description) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Description persistentDescription = em.find(Description.class, description.getDescriptionId());
            Entry entryIdOld = persistentDescription.getEntryId();
            Entry entryIdNew = description.getEntryId();
            if (entryIdNew != null) {
                entryIdNew = em.getReference(entryIdNew.getClass(), entryIdNew.getEntryId());
                description.setEntryId(entryIdNew);
            }
            description = em.merge(description);
            if (entryIdOld != null && !entryIdOld.equals(entryIdNew)) {
                entryIdOld.getDescriptionList().remove(description);
                entryIdOld = em.merge(entryIdOld);
            }
            if (entryIdNew != null && !entryIdNew.equals(entryIdOld)) {
                entryIdNew.getDescriptionList().add(description);
                entryIdNew = em.merge(entryIdNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = description.getDescriptionId();
                if (findDescription(id) == null) {
                    throw new NonexistentEntityException("The description with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Description description;
            try {
                description = em.getReference(Description.class, id);
                description.getDescriptionId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The description with id " + id + " no longer exists.", enfe);
            }
            Entry entryId = description.getEntryId();
            if (entryId != null) {
                entryId.getDescriptionList().remove(description);
                entryId = em.merge(entryId);
            }
            em.remove(description);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Description> findDescriptionEntities() {
        return findDescriptionEntities(true, -1, -1);
    }

    public List<Description> findDescriptionEntities(int maxResults, int firstResult) {
        return findDescriptionEntities(false, maxResults, firstResult);
    }

    private List<Description> findDescriptionEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Description.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Description findDescription(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Description.class, id);
        } finally {
            em.close();
        }
    }

    public int getDescriptionCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Description> rt = cq.from(Description.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
